package org.nwiz.calc;

import java.util.Scanner;

public class Calc {
    public static void main(String args[]) {
        if (args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                String expr = args[i];
                try {
                    System.out.println(
                            String.format("(%d) %s = %f", 1 + i, expr, Eval.eval(expr)));
                } catch (Exception e) {
                    System.out.println(
                            String.format("Illegal expression: %s", expr));
                }
            }
        } else {
            int i = 1;
            Scanner scan = new Scanner(System.in);
            while (true) {
                String expr;
                try {
                    expr = scan.next();
                } catch (Exception e) {
                    return;
                }
                if (expr.length() <= 0)
                    break;
                try {
                    System.out.println(String.format("(%d) %s = %f", i, expr, Eval.eval(expr)));
                    i += 1;
                } catch (Exception e) {
                    System.out.println(
                            String.format("Illegal expression: %s", expr));
                }

            }

        }
    }
}
